package com.qianfeng.www.day10image;

import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;

import com.bumptech.glide.Glide;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.view.SimpleDraweeView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.qianfeng.www.day10image.config.NetConfig;
import com.squareup.picasso.Picasso;

public class MainActivity extends AppCompatActivity {
    private ImageButton mUilImageButton,mPicasoImageButton,mGlideImageButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Fresco.initialize(this);
        setContentView(R.layout.activity_main);
        initView();
        //Imageloader配置信息
        ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(this)
                // 设置内存缓存的大小
                .memoryCacheSize(4).
                 //设置线程池的大小
                 threadPoolSize(5).
                   //设置缓存文件的数量
                  discCacheFileCount(50).build();
        // 调用iamgeloader的时候必须调用 （初始化）
        ImageLoader.getInstance().init(configuration);

        ///////////////fresco////////////
        // 如果遇到满屏幕的gif图 那么Xutils加载会卡顿，glide加载也会卡（但是会好一点），Picasso（不能加载gif）只有fresco才能保证不卡顿
        Uri uri = Uri.parse(NetConfig.UIL_PATH);
        SimpleDraweeView draweeView = (SimpleDraweeView) findViewById(R.id.sv_mianactivity_test);
        draweeView.setImageURI(uri);

    }

    private void initView() {
        mGlideImageButton = (ImageButton) findViewById(R.id.bt_mainactivity_glide);
        mPicasoImageButton = (ImageButton) findViewById(R.id.bt_mainactivity_picaso);
        mUilImageButton = (ImageButton) findViewById(R.id.bt_mainactivity_uil);
    }

    public void onClick(View view){
        if (view!=null){
            switch (view.getId()){
                case R.id.bt_mainactivity_uil:
                    // 设置每一张图片缓冲的选项
                    DisplayImageOptions options = new DisplayImageOptions.Builder().cacheOnDisc(true).cacheInMemory(true).build();
                    ImageLoader.getInstance().displayImage(NetConfig.UIL_PATH, mUilImageButton,options);
                    break;
                case R.id.bt_mainactivity_picaso:
                    Picasso.with(this).load(NetConfig.UIL_PATH).into(mPicasoImageButton);
                    //Picasso.with(this).load(NetConfig.UIL_PATH).priority(Picasso.Priority.HIGH).
                    break;
                case R.id.bt_mainactivity_glide:
                    Glide.with(this).load(NetConfig.UIL_PATH).into(mGlideImageButton);
                    break;
                default:
                    break;
            }
        }
    }
}
